package com.dindang.ddm.mvp.ui.adapter.multitype;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dindang.ddm.R;
import com.dindang.ddm.mvp.model.entity.Backlog;
import com.dindang.ddm.mvp.model.entity.Message;
import com.jess.arms.utils.ArmsUtils;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class MessageViewBinder extends ItemViewBinder<Message, MessageViewBinder.ViewHolder> {

    private int[] stateColors = {R.color.dd_color_blue,R.color.dd_color_origin,R.color.dd_color_green,R.color.dd_color_red,};

    OnClickListener listener;
    public MessageViewBinder(OnClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_message2, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Message message) {
        switch (message.state) {
            case 0:
                holder.tv_title.setVisibility(View.VISIBLE);
                holder.ll_repair_man.setVisibility(View.VISIBLE);
                holder.tv_repair_man.setVisibility(View.VISIBLE);
                holder.ll_fault_address.setVisibility(View.VISIBLE);
                holder.tv_fault_address.setVisibility(View.VISIBLE);
                holder.ll_team_man.setVisibility(View.VISIBLE);
                holder.tv_team_man.setVisibility(View.VISIBLE);
                holder.ll_repair_area.setVisibility(View.VISIBLE);
                holder.tv_repair_area.setVisibility(View.VISIBLE);
                holder.ll_fault_type.setVisibility(View.VISIBLE);
                holder.tv_fault_type.setVisibility(View.VISIBLE);
                holder.ll_order_no.setVisibility(View.VISIBLE);
                holder.tv_order_no.setVisibility(View.VISIBLE);
                holder.tv_content.setVisibility(View.GONE);
                holder.tv_title.setText("有1个新的报修单需要你派单处理");
                holder.tv_repair_man.setText(message.repairMan);
                holder.tv_fault_address.setText(message.faultAddress);
                if (!TextUtils.isEmpty(message.teamMan)) {
                    holder.ll_team_man.setVisibility(View.VISIBLE);
                }else{
                    holder.ll_team_man.setVisibility(View.GONE);
                }
                holder.tv_team_man.setText(message.teamMan);
                holder.tv_repair_area.setText(message.repairArea);
                holder.tv_fault_type.setText(message.faultTypes);
                holder.tv_order_no.setText(message.orderNo);
                holder.tv_state.setText("待接单");
                break;
            case 1:
                holder.tv_title.setVisibility(View.GONE);
                holder.ll_repair_man.setVisibility(View.GONE);
                holder.ll_fault_address.setVisibility(View.GONE);
                holder.ll_team_man.setVisibility(View.GONE);
                holder.ll_repair_area.setVisibility(View.GONE);
                holder.ll_fault_type.setVisibility(View.GONE);
                holder.ll_order_no.setVisibility(View.GONE);
                holder.tv_content.setVisibility(View.VISIBLE);

                StringBuffer content = new StringBuffer();
                content.append("报修单号为" + message.orderNo);
                content.append("，报修人员为" + message.repairMan);
                content.append(" " + message.faultAddress + "的报修单已被" + message.takeOrderMan + "接单处理。");
                holder.tv_content.setText(content);
                holder.tv_state.setText("已接单");
                break;
            case 2:
                holder.tv_title.setVisibility(View.GONE);
                holder.ll_repair_man.setVisibility(View.GONE);
                holder.ll_fault_address.setVisibility(View.GONE);
                holder.ll_team_man.setVisibility(View.GONE);
                holder.ll_repair_area.setVisibility(View.GONE);
                holder.ll_fault_type.setVisibility(View.GONE);
                holder.ll_order_no.setVisibility(View.GONE);
                holder.tv_content.setVisibility(View.VISIBLE);

                content = new StringBuffer();
                content.append("报修单号为" + message.orderNo);
                content.append("，报修人员为" + message.repairMan);
                content.append(" " + message.faultAddress + "的报修单已被" + message.takeOrderMan + "完成处理。");
                holder.tv_content.setText(content);
                holder.tv_state.setText("已完成");
                break;
            case 3:
                holder.tv_title.setVisibility(View.GONE);
                holder.ll_repair_man.setVisibility(View.GONE);
                holder.ll_fault_address.setVisibility(View.GONE);
                holder.ll_team_man.setVisibility(View.GONE);
                holder.ll_repair_area.setVisibility(View.GONE);
                holder.ll_fault_type.setVisibility(View.GONE);
                holder.ll_order_no.setVisibility(View.GONE);
                holder.tv_content.setVisibility(View.VISIBLE);

                content = new StringBuffer();
                content.append("报修单号为" + message.orderNo);
                content.append("，报修人员为" + message.repairMan);
                content.append(" " + message.faultAddress + "的报修单已被" + message.takeOrderMan + "撤销处理。");
                holder.tv_content.setText(content);
                holder.tv_state.setText("已撤销");
                break;
        }
        holder.tv_state.setTextColor(ArmsUtils.getColor(holder.tv_state.getContext(), stateColors[message.state]));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(holder.getAdapterPosition());
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tv_title;
        LinearLayout ll_repair_man;
        TextView tv_repair_man;
        LinearLayout ll_fault_address;
        TextView tv_fault_address;
        LinearLayout ll_team_man;
        TextView tv_team_man;
        LinearLayout ll_repair_area;
        TextView tv_repair_area;
        LinearLayout ll_fault_type;
        TextView tv_fault_type;
        LinearLayout ll_order_no;
        TextView tv_order_no;
        TextView tv_content;
        TextView tv_state;

        ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            ll_repair_man = itemView.findViewById(R.id.ll_repair_man);
            tv_repair_man = itemView.findViewById(R.id.tv_repair_man);
            ll_fault_address = itemView.findViewById(R.id.ll_fault_address);
            tv_fault_address = itemView.findViewById(R.id.tv_fault_address);
            ll_team_man = itemView.findViewById(R.id.ll_team_man);
            tv_team_man = itemView.findViewById(R.id.tv_team_man);
            ll_repair_area = itemView.findViewById(R.id.ll_repair_area);
            tv_repair_area = itemView.findViewById(R.id.tv_repair_area);
            ll_fault_type = itemView.findViewById(R.id.ll_fault_type);
            tv_fault_type = itemView.findViewById(R.id.tv_fault_type);
            ll_order_no = itemView.findViewById(R.id.ll_order_no);
            tv_order_no = itemView.findViewById(R.id.tv_order_no);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_state = itemView.findViewById(R.id.tv_state);
        }
    }
    public interface OnClickListener {
        void onClick(int position);
    }
}
